// pages/me_all/updateUserInfo/updateUserInfo.js
const app=getApp()
var IDpass=true
var namePass=true
var phonePass=true
var isConfirm=false
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:{},
        showarea:false,
        address:{},
        addressString:'',
        code:"",
        Name:"",
        phone:"",
        isConfirm:false
    },
    getName(e) {
        namePass = true;
        console.log(e.detail)
        this.setData({ Name: e.detail })
        let reg = /^[\u2E80-\u9FFF]+$/;
        console.log(reg.test(e.detail))
        if (!reg.test(e.detail)) {
          console.log("执行")
          this.setData({ nameErrMes: "姓名格式错误" })
          namePass = false;
        }
        else {
          this.setData({ nameErrMes: "" })
          namePass = true;
        }
      },
      //获取身份证号
      getIDNum(e) {
        console.log(e.detail);
        this.setData({ code: e.detail })
        this.identityCodeValid()
      },
      //检测身份证号的合法性
      identityCodeValid() {
        IDpass = true;
        var city = {
          11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 "
        };
        var idCardReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/g;
        var code = this.data.code
        if (!code || !idCardReg.test(code)) {
          this.setData({ IDNumErrMes: "身份证号码错误，请重新输入" })
          IDpass = false;
        } else if (!city[code.substr(0, 2)]) {
          this.setData({ IDNumErrMes: "身份证号码错误，请重新输入" })
          IDpass = false;
        } else {
          //18位身份证需要验证最后一位校验位
          if (code.length == 18) {
            code = code.split('');
            //∑(ai×Wi)(mod 11)
            //加权因子
            var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            //校验位
            var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
            var sum = 0;
            var ai = 0;
            var wi = 0;
            for (var i = 0; i < 17; i++) {
              ai = code[i];
              wi = factor[i];
              sum += ai * wi;
            }
            var last = parity[sum % 11];
            if (parity[sum % 11] != code[17]) {
              this.setData({ IDNumErrMes: "身份证号码错误，请重新输入" })
              IDpass = false;
            }
          }
        }
        if (IDpass) this.setData({ IDNumErrMes: "" })
      },
      checkphone() {
        phonePass = true;
        var phoneReg = /^1[3456789]\d{9}$/
        if (!phoneReg.test(this.data.phone)) {
          this.setData({ phoneErrMes: "手机号码格式错误,请重新输入" })
          phonePass = false;
        }
        else {
          this.setData({ phoneErrMes: "" })
          phonePass = true;
        }
      },
      getPhone(e) {
        this.setData({ phone: e.detail })
        this.checkphone()
      },
      update(){
          console.log(IDpass)
          console.log(namePass)
          console.log(phonePass)
          if(!isConfirm)
          {
            wx.showToast({
              title: '请先勾选同意隐私协议',
              icon:"none"
            })
          }
          else{
            this.identityCodeValid()
            this.checkphone()
            if(IDpass&&namePass&&phonePass)
            {
                wx.cloud.callFunction({
                    name:"updateUserInfo",
                    data:{
                        action:"userInfo",
                        openid:app.globalData.openid,
                        Name:this.data.Name,
                        idNUm:this.data.code,
                        phone:this.data.phone,
                        regionDetail:this.data.addressString
                    }
                })
                .then(res=>{
                  console.log("志愿者更新信息成功",res)
                  wx.showToast({
                    title: '更新信息成功',
                  })
                  wx.switchTab({
                    url: '../../me/me',
                  })
                })
            }
          }
        
        
    },
    //打开地址选择器
    openAddress(){
        this.setData({
            showarea:true,
            address:this.data.address
        })
    },
    /*地址选择器监听事件：
    触发条件：所选地址与先前地址不同，选够五级地址，e.detail.address为返回的五级地址对象*/
    async changeAddress(e){
        console.log('所选地址与先前地址不一致',e.detail);
        let address = e.detail.address;
        let addressString = address.province+address.city+address.country+address.street+address.village;
        this.data.address = address;
        this.setData({
                addressString:addressString
        })
        await wx.cloud.callFunction({
                name:"updateUserInfo",
                data:{
                  action:"userInfo",
                  openid:app.globalData.openid,
                  regionDetail:addressString,
                  regionDetailObject:address,
                }
        })    
        
    },
    tapToConfirm(){
      this.setData({
        isConfirm:!isConfirm
      })
      isConfirm=!isConfirm
    },
    PrivatePro(){
       wx.navigateTo({
         url: '../../PrivatePro/PrivatePro',
       })
  
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(app.globalData.userInfo)
    this.setData({
        userInfo:app.globalData.userInfo.userInfo,
        code:app.globalData.userInfo.userInfo.idNum, Name:app.globalData.userInfo.userInfo.name,
        phone:app.globalData.userInfo.userInfo.phone,
        addressString:app.globalData.userInfo.userInfo.regionDetail,
        address:app.globalData.userInfo.userInfo.regionDetailObject
    })
    },
    tapToConfirm(){
        this.setData({
          isConfirm:!isConfirm
        })
        isConfirm=!isConfirm
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})